﻿namespace CRM.DataAccess.DAOs
{
    using System.Collections.Generic;
    using System.Data.Entity;
    using CRM.DataAccess.Entities;
    using System.Linq;
    using System;

    public partial class LoyaltyDAO : DbContext
    {
        
        public LoyaltyDAO()
            : base("name=Entities")
        {
            
        }
        public DbSet<LOYALTY> LOYALTIES { get; set; }
               
        //Get default Loyalty
        public LOYALTY GetDefaultLoyalty(long? assignedToID)
        {
            try
            {
                var lQuery = (from l in LOYALTIES
                              where l.ASSIGN_TO_ID == (assignedToID == null ? l.ASSIGN_TO_ID : assignedToID)
                              select l).FirstOrDefault<LOYALTY>();
                return lQuery as LOYALTY;
            }
            catch
            {
                return null;
            }
        }
        //Get Loyalty by Name
        public LOYALTY GetLoyaltyByName(string name, long? assignedToID)
        {
            try
            {
                var lQuery = (from l in LOYALTIES
                              where l.ASSIGN_TO_ID == (assignedToID == null ? l.ASSIGN_TO_ID : assignedToID)
                              && l.NAME.ToUpper() == name.ToUpper()
                              select l).FirstOrDefault<LOYALTY>();
                return lQuery as LOYALTY;
            }
            catch
            {
                return null;
            }
        }
        //Get Loyalty by ID
        public LOYALTY GetLoyaltyByID(long ID, long? assignedToID)
        {
            try
            {
                var lQuery = (from l in LOYALTIES
                              where l.ASSIGN_TO_ID == (assignedToID == null ? l.ASSIGN_TO_ID : assignedToID)
                              && l.ID == ID
                              
                              select l).FirstOrDefault<LOYALTY>();
                return lQuery as LOYALTY;
            }
            catch
            {
                return null;
            }
        }
        //Get previous Loyalty by points to award
        public List<LOYALTY> GetPreviousLoyaltyByPointsToAward(double? pointsToAward, long? assignedToID)
        {
            try
            {
                var lQuery = (from l in LOYALTIES
                              where l.ASSIGN_TO_ID == (assignedToID == null ? l.ASSIGN_TO_ID : assignedToID)
                              && l.POINTS_TO_AWARD < pointsToAward
                              select l);
                List<LOYALTY> results = lQuery.ToList();
                if (results.Count > 0)
                {
                    return GetDescLoyaltyList(results);
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        private List<LOYALTY> GetDescLoyaltyList(List<LOYALTY> input)
        {
            if (input != null && input.Count > 0)
            {
                for (int i = 0; i < input.Count; i++)
                {
                    for (int j = 0; j < input.Count; j++)
                    {
                        if (input[i].POINTS_TO_AWARD > input[j].POINTS_TO_AWARD)
                        {
                            LOYALTY temp = input[i];
                            input[i] = input[j];
                            input[j] = temp;
                        }
                    }
                }
            }
            return input;
        }
        private List<LOYALTY> GetAscLoyaltyList(List<LOYALTY> input)
        {
            if (input != null && input.Count > 0)
            {
                for (int i = 0; i < input.Count; i++)
                {
                    for (int j = 0; j < input.Count; j++)
                    {
                        if (input[i].POINTS_TO_AWARD < input[j].POINTS_TO_AWARD)
                        {
                            LOYALTY temp = input[i];
                            input[i] = input[j];
                            input[j] = temp;
                        }
                    }
                }
            }
            return input;
        }
        //Get next Loyalty by points to award
        public List<LOYALTY> GetNextLoyaltyByPointsToAward(double? pointsToAward, long? assignedToID)
        {
            try
            {
                var lQuery = (from l in LOYALTIES
                              where l.ASSIGN_TO_ID == (assignedToID == null ? l.ASSIGN_TO_ID : assignedToID)
                              && l.POINTS_TO_AWARD > pointsToAward
                              select l);
                List<LOYALTY> results = lQuery.ToList();
                if (results.Count > 0)
                {
                    return GetAscLoyaltyList(results);
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        ///Get loyalty by condition
        public List<LOYALTY> GetLoyaltyByCondition(DateTime? dateFrom, DateTime? dateTo, string name, double? pointToAward, double? exchangeRate, long? assignedToID)
        {
            try
            {
                var lQuery = from l in LOYALTIES
                             where l.ASSIGN_TO_ID == (assignedToID == null ? l.ASSIGN_TO_ID : assignedToID)
                             && (l.DATE_CREATED >= dateFrom && l.DATE_CREATED < dateTo)
                             && (name != "" ? (l.NAME.ToUpper().Contains(name.ToUpper())) : (true))
                         //    && (l.NAME.ToUpper() == (name == "" ? l.NAME.ToUpper() : name.ToUpper()))
                             && (l.POINTS_TO_AWARD == (pointToAward == null ? l.POINTS_TO_AWARD : pointToAward))
                             && (l.EXCHANGE_RATE == (exchangeRate == null ? l.EXCHANGE_RATE : exchangeRate))
                             select l;
                List<LOYALTY> ls = lQuery.AsNoTracking().ToList();
                if (ls.Count > 0)
                    return ls;
                return null;
            }
            catch
            {
                return null;
            }
        }
        /// Lists all.
        public List<LOYALTY> ListAll(long? assignedToID)
        {
            try
            {
                var lQuery = from l in LOYALTIES
                                where l.ASSIGN_TO_ID == (assignedToID == null ? l.ASSIGN_TO_ID : assignedToID)
                                select l;
                List<LOYALTY> ls = lQuery.AsNoTracking().ToList();
                if (ls.Count > 0)
                    return GetAscLoyaltyList(ls);
                return null;
            }
            catch
            {
                return null;
            }
        }
        //Get LOYALTY by ID
        public LOYALTY GetLoyaltyByID(long ID)
        {
            try
            {
                var lQuery = (from l in LOYALTIES
                                where l.ID == ID
                                select l).FirstOrDefault<LOYALTY>();
                return lQuery as LOYALTY;
            }
            catch 
            { 
                return null; 
            }
        }
        //Add new loyalty
        public int AddNewLoyalty(LOYALTY l)
        {
            try
            {
                this.LOYALTIES.Add(l);
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
       
        //Delte loyalty
        public int DeleteLoyalty(long ID)
        {
            try
            {
                this.LOYALTIES.RemoveRange(LOYALTIES.Where(x => x.ID == ID));
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        //Update user
        public int UpdateLoyalty(LOYALTY l)
        {
            try
            {
                var original = LOYALTIES.Find(l.ID);
                if (original != null)
                {
                    Entry(original).CurrentValues.SetValues(l);
                    SaveChanges();
                    return 1;
                }
                return -1;
            }
            catch
            {
                return -1;
            }
        }
    }
}
